package com.example.controller;

import com.example.data.attendance.Attendance;
import com.example.data.attendance.AttendanceVO;
import com.example.exception.BadRequestException;
import com.example.service.attendance.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

@RestController
@RequestMapping(value = "/AttendanceSystem")
public class AttendanceController {
    private AttendanceService attendanceServiceImpl;
    @Autowired
    public AttendanceController(AttendanceService attendanceServiceImpl){
        this.attendanceServiceImpl = attendanceServiceImpl;
    }
    @GetMapping("/attendance")
    public List<Attendance> getAllAttendance(){
        return attendanceServiceImpl.getAllAttendance();
    }
    @GetMapping("/attendance/id")
    public List<Attendance> getAttendanceByID(@RequestParam(value = "empID", required = false) Long empID,
                                              @RequestParam(value = "attendanceID", required = false) Long attendanceID){
        BadRequestException.checkAllNull(empID, attendanceID);
        return attendanceServiceImpl.getAttendanceByID(empID, attendanceID);
    }
    //    @PostMapping("/attendance")
//    public List<Attendance> addAttendance(@RequestBody Attendance attendance){
//        BadRequestException.checkHasNull(AttendanceVO.AttendanceList(attendance));
//        return attendanceServiceImpl.addAttendance(AttendanceVO.convert(attendance));
//    }
    @PostMapping("/attendance")
    @ResponseBody
    public ResponseEntity importAttendance(@RequestParam(value = "file") MultipartFile file) throws IOException {
        InputStream in = file.getInputStream();
        String OriginalFileName = file.getOriginalFilename();
        String fileName = file.getName();
        String path = AttendanceController.class.getResource("/").getPath();
        File f = new File(new File(path).getAbsolutePath()+ "/" + fileName);
        file.transferTo(f);
        return attendanceServiceImpl.importAttendance(f);
    }
    @DeleteMapping("/attendance")
    public ResponseEntity<String> deleteAttendance(@NonNull @RequestParam Long attendanceID){
        return attendanceServiceImpl.deleteAttendance(attendanceID);
    }
}
